﻿namespace Cubezicles.UI
{
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    public class EmployeeManagerPane : ManagerPane
    {
        private EmployeeManager em; 

        // Textures
       // Texture2D btn; in base class
        Texture2D mood;
        Rectangle moodR, moodhoverR;

        //List<Button> controls; in base class
        private Button hireEmp; //, okButton; base class

        public EmployeeManagerPane(Game1 game, EmployeeManager empMan) : base(game)//, xPos, yPos, w, h, title)
        {
            em = empMan;

            int xPos = 75;
            int yPos = 50;
            int w = game.screenW - 150;
            int h = game.screenH - 180;

            PanePosition = new Vector2(xPos, yPos);
            PaneSize = new Vector2(w, h);
            FrameTitle = game.Messages["EmployeePaneTitle"];//"Employee Management";

            //IsVisible = false;  // todo: why is this isntantly showoing on startup.....???*******
        }


        protected override void LoadContent()
        {
            butts = Game.Content.Load<Texture2D>("UI/buttons");
            mood = Game.Content.Load<Texture2D>("UI/emp-mood");
            moodR = new Rectangle(0, 0, 269, 64);
            moodhoverR = new Rectangle(0, 64, 269, 64);
            
            hireEmp = new Button()
            {
                Texture = butts,
                text = "Hire new employee!",
                xpos = game.screenW - 230,
                ypos = 50 + 20,
                // should open up a new pane to choose employee to hire....*****
                OnClick = (cont) =>
                {
                    //Open the new employee pane
                    //e.newHireManagerPane.IsVisible = !e.newHireManagerPane.IsVisible;
                    em.HireNew();
                }
            };
            /*
            okButton = new Button()
            {
                Texture = butts,
                text = "Okay",
                xpos = 120,
                ypos = 15 + 70 * 6,
                active = true,
                OnClick = (cont) => { IsVisible = false; }
            };
            okButton.Enable();
            */
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (Employee emp in em.Employees)
            {
                emp.DrawThoughtBubble();
            }

            if (IsVisible)
            {
                // Create a list of UI controls
                controls = new List<Button>(); // variable in base class

                // Get the screen width and height
                int screenH = GraphicsDevice.Viewport.Height;
                int screenW = GraphicsDevice.Viewport.Width;

                game.SpriteBatch.Begin();

                // Draw the frame that holds the info
                int y = 50;

                // height to draw reason, should be right under hire button
                int reasonErrorX = screenW - 220 + 20;
                int reasonErrorY = y + 45;

                // amount of space between text description and start of employee listings boxes
                const int spaceBtwListings = 63;

                DrawFrame();
                
                // disable the second part because employees have diff costs now
                if (em.NextEmployeeID > 9)// || game.MoneyManager.CurrentFunds < MoneyManager.CostHireBonus)
                {
                    hireEmp.Active = false; // Disable the button - player can't hire employee
                    //game.SpriteBatch.Draw(btn, new Vector2(screenW - 220, y + 15), new Rectangle(0, 61, 132, 100 / 3), Color.White);

                    // Display why the player can't hire an employee.

                    // If there were not sufficient funds, display a notification to the user //if (game.MoneyManager.CurrentFunds < MoneyManager.CostHireBonus)
                    string err = "Can't afford!";
                    Color errColor = Color.Red; // MediumVioletRed is pretty! can we use it somewhere?!

                    // Check for this one second, vs. vice versa, because if a player already
                    // has 10 employees and doesn't have sufficient funds to hire, the reason the hire button
                    // is disabled is because they've reached the max, not because of funds
                    if (em.Employees.Count >= 9)
                    {
                        err = "Max Capacity!"; // Player has reached the maximum number of employees
                        errColor = Color.Gray;
                    }

                    game.SpriteBatch.DrawString(game.Fonts["info"], err, new Vector2(reasonErrorX, reasonErrorY), errColor); 
                }
                else
                {
                    hireEmp.Active = true; // Enable the button - player can hire the employee
                }

                //--- hireEmp new employee button ---//
                hireEmp.font = game.Fonts["info"];
                controls.Add(hireEmp);

                y += spaceBtwListings; // 63; //was45; amount of space between text description and start of employee listings boxes
                
                // lcX and rcX = left column x and right column x
                const int leftColX = 100;
                const int rightColX = 400;

                int x = leftColX;
                // For each employee in Employees
                foreach (var employee in em.Employees)
                {
                    if (employee != null)
                    {
                        // Draw their "employee listing"
                        DrawEmployeeListing(employee, x, y);
                        if (x == leftColX) x = rightColX;
                        else
                        {
                            x = leftColX;
                            y += spaceBtwListings; //62 (now 63) was 70... vertical space between employee listings
                        }
                    }
                }

                //--- Draw the okay button ---//
                int xpos = 120;
                int ypos = 15 + 70 * 6;
                DrawOKbutton(xpos, ypos); // in base class

                // Draw ALL the buttons
                controls.ForEach(control => control.Update(GraphicsDevice.Viewport, game.ms, game.oldms));
                controls.ForEach(control => control.Draw(game.SpriteBatch, GraphicsDevice.Viewport));

                game.SpriteBatch.End();
            }
        }

        private void DrawEmployeeListing(Employee emp, int x, int y)
        {
            // Add listing "button"/tile
            Rectangle bb = new Rectangle(x, y, moodR.Width, moodR.Height);
            Rectangle msp = new Rectangle(game.ms.X, game.ms.Y, 1, 1);
            if (bb.Intersects(msp))
            {
                if (game.ms.LeftButton == ButtonState.Pressed)
                {
                    game.CurrentEmployee = emp;
                }
            }

            // Check selectedness
            if (emp.IsSelected)
            {
                // If the employee is selected, their Employee Listing has a shadow to emphasize it
                game.SpriteBatch.Draw(mood, new Vector2(x, y), moodhoverR, Color.White);

                // Only display the fire button for the currently selected employee in the employeemanagerpane
                // This simplifies the code to call (don't have to figure out which employee button belongs to)
                DrawFireButton(x, y, emp);
            }

            game.SpriteBatch.Draw(mood, new Vector2(x, y), moodR, NeedsDisplay.determineColor(emp.OverallMood));

            // Draw the profile pic/avatar thumbnail image/thingy of character's face/facial expression
            int z = 44; //width and height of profile image
            game.SpriteBatch.Draw(emp.GetFace(), new Rectangle(x + 10, y + 10, z, z), Color.White);

            // Draw the text that indicates their current State (working, havingfun, etc.)
            game.SpriteBatch.DrawString(game.Fonts["status"], emp.Name, new Vector2(x + 70, y + 15), Color.White);

            string status = "Status: " + emp.GetStateString();
            game.SpriteBatch.DrawString(game.Fonts["status"], status, new Vector2(x + 205 - game.Fonts["status"].MeasureString(status).X, y + 35), Color.White);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="emp"> Currently selected employee to fire </param>
        private void DrawFireButton(int x, int y, Employee emp)
        {
            // Fire button
            //fireEmp = new Button()
            controls.Add(new Button()
            {
                Texture = butts,
                font = game.Fonts["info"],
                text = "Fire!",
                xpos = x + 200,
                ypos = y + 25,
                btnColor = Color.Red, //Get the colour to work properly.
                OnClick = (cont) => em.Fire(emp) // Fire them!
            });
        }
    }
}
